const themeHtml = {
    /*多图上传*/
    multi01: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row" style="background:url(${row.file_url}) no-repeat center center / cover;">
            <input type="hidden" name="${name}[${row.id}][id]" value="${row.id}" />
            <input type="hidden" class="upload-url" name="${name}[${row.id}][file_url]" value="${row.file_url}" />
            <div class="icon-bar">
                <i class="mdi mdi-arrow-left text-white" onclick="helper.upMove(this)"></i>
                <i class="mdi mdi-arrow-right text-white" onclick="helper.downMove(this)"></i>
                <i class="mdi mdi-delete text-white" onclick="themeCallfn.multiDel(this)"></i>
                <i class="mdi mdi-eye text-white" onclick="themeCallfn.multiShow(this)"></i>
            </div>
        </li>
        {@/each}
    */}),
    /*多图带描述*/
    multi02: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row">
            <div class="g-bd">
                <div class="g-sd">
                    <div class="image" style="background:url(${row.file_url}) no-repeat center center / cover;">
                        <input type="hidden" name="${name}[${row.id}][id]" value="${row.id}" />
                        <input type="hidden" class="upload-url" name="${name}[${row.id}][file_url]" value="${row.file_url}" />
                        <div class="icon-bar">
                            <i class="mdi mdi-arrow-left text-white" onclick="helper.upMove(this)"></i>
                            <i class="mdi mdi-arrow-right text-white" onclick="helper.downMove(this)"></i>
                            <i class="mdi mdi-delete text-white" onclick="themeCallfn.multiDel(this)"></i>
                            <i class="mdi mdi-eye text-white" onclick="themeCallfn.multiShow(this)"></i>
                        </div>
                    </div>
                </div>
                <div class="g-mn">
                    <div class="g-mnc">
                        <input type="text" class="form-control input-sm" name="${name}[${row.id}][s1]" value="${row.s1}" />
                        <textarea class="form-control input-sm" name="${name}[${row.id}][s2]" style="height:60px;">${row.s2}</textarea>
                    </div>
                </div>
            </div>
        </li>
        {@/each}
    */}),
    /*多附件上传*/
    multi03: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row">
            <input type="hidden" name="${name}[${row.id}][id]" value="${row.id}" />
            <input type="hidden" name="${name}[${row.id}][file_ext]" value="${row.file_ext}" />
            <div class="file-ext ${row.file_ext}"></div>
            <div class="file-info">
                <div class="input-group m-b-10">
                    <span class="input-group-addon">名称</span>
                    <input type="text" class="form-control" name="${name}[${row.id}][original_name]" value="${row.original_name}" placeholder="文件名称">
                </div>
                <div class="input-group m-b-10">
                    <input type="text" class="form-control" name="${name}[${row.id}][file_size]" value="${row.file_size}">
                </div>
            </div>
            <div class="input-group">
                <span class="input-group-addon">路径</span>
                <input type="text" class="form-control upload-url" name="${name}[${row.id}][file_url]" value="${row.file_url}" readonly>
            </div>
            <div class="icon-bar">
                <i class="mdi mdi-arrow-up" onclick="helper.upMove(this)"></i>
                <i class="mdi mdi-arrow-down" onclick="helper.downMove(this)"></i>
                <i class="mdi mdi-delete" onclick="themeCallfn.multiDel(this)"></i>
            </div>
        </li>
        {@/each}
    */})
};
const themeCallfn = {
    singleShow: function (e) {
        let dom = $(e).closest('.f-upload');
        let file_url = dom.find('.file-url').val();
        if (valid.isEmpty(file_url)) {
            helper.tips('请上传图片');
            return false;
        }
        layer.photos({
            photos: {
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {"src": file_url}
                ]
            }
        });
    },
    //无需模板
    single01: function (res, obj) {
        $(obj).find('.file-url').val(res[0].file_url);
    },
    //无需模板,资源库没有的情况
    single02: function (res, obj) {
        let fileUrl = res;
        if(typeof res=='object') fileUrl = res[0].file_url;
        $(obj).find('.file-url').val(fileUrl);
    },
    //删除附件
    single01Del: function (e) {
        let dom = $(e).closest('.f-upload');
        let file_url = dom.find('.file-url').val();
        if (valid.isEmpty(file_url)) {
            helper.tips('请上传图片');
            return false;
        }
        dom.find('.file-url').val('');
    },
    //多图上传删除
    multiDel: function (e) {
        $(e).closest('.item-row').remove();
    },
    multiShow: function (e) {
        let data = [];
        let start = 0;
        let selfItemRow = $(e).closest('.item-row');
        let itemRow = $(e).closest('.f-upload').find('.item-row');
        itemRow.removeClass('show-active');
        selfItemRow.addClass('show-active');
        itemRow.each(function (i) {
            let url = $(this).closest('.item-row').find('.upload-url').val();
            if($(this).closest('.item-row').hasClass('show-active')) start=i;
            data.push({src: url});
        });
        layer.photos({
            photos: {
                "start": start, //初始显示的图片序号，默认0
                "data": data
            }
        });
    },
    multi: function (list, obj) {
        let option = obj.data();
        let name = option.name || 'upload';
        let theme = option.theme;
        let html = juicer(themeHtml[theme], {list: list, name: name});
        obj.find('.upload-list').append(html);
    },
    selectFile: function (e) {
        let obj = $(e).closest('.f-upload');
        let option = obj.data();
        let type = $(e).data('type') || 'single';
        let file = $(e).data('file') || '';
        let url = '/system/attachment/select.html?type=' + type + '&file_type=' + file;
        let bodyWidth = $(document.body).outerWidth();
        let bodyHeight = $(document.body).outerHeight();
        let width = bodyWidth<980 ? '100%' : '980px';
        let height = bodyHeight<730 ? '100%' : '730px';
        layer.open({
            type: 2,
            title: '选择图片',
            area: [width, height],
            btn: ['确定'],
            content: url,
            yes: function (index, layero) {
                let iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                let list = iframeWin.doAction.subFile();
                let theme = option.theme;
                let Fun = option.themeFun || theme;
                eval('themeCallfn.' + Fun + '(list,obj)');
                layer.close(index);
            }
        });
    }
};